package com.wlts.wzy.DYOrderModule.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.linkedmall.model.v20180116.QueryOrderListRequest;
import com.aliyuncs.linkedmall.model.v20180116.QueryOrderListResponse;
import com.wlts.wzy.DYOrderModule.entity.DyOrders;
import com.wlts.wzy.DYOrderModule.service.IDyOrdersService;
import com.wlts.wzy.DYUserModule.entity.DyStoreUser;
import com.wlts.wzy.DYUserModule.service.IDyStoreUserService;
import com.wlts.wzy.LMGoodsModule.service.ILmAddressService;
import com.wlts.wzy.LMGoodsModule.service.ILmSelfGoodsService;
import com.wlts.wzy.configBean.LmConfig.LmConfig;
import com.wlts.wzy.expand.MyLambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;

import static com.wlts.wzy.configBean.LmConfig.LmConfig.client;

@Component
public class LmUpdateDyOrdersToLocalUtil {
    @Autowired
    private IDyOrdersService ksOrdersService;
    @Autowired
    private ILmSelfGoodsService lmSelfGoodsService;
    @Autowired
    private ILmAddressService lmAddressService;
    @Autowired
    private IDyStoreUserService ksStoreUserService;

    /**
     * 获得发货订单--更新订单
     */
    @Scheduled(fixedDelay = 211 * 1000)
//    @PostConstruct
    public void LmUpdateOrdersToLocal() throws ClientException {

        System.out.println("获得发货订单--更新订单 -- 计时器");

        MyLambdaQueryWrapper<DyStoreUser> userWrapper = new MyLambdaQueryWrapper<>();

        userWrapper.eq(DyStoreUser::getDelFlag, "0");

        userWrapper.isNotNull(DyStoreUser::getDySellerId);

        List<DyStoreUser> ksOauthList = ksStoreUserService.list(userWrapper);

        for (DyStoreUser user : ksOauthList
        ) {
            MyLambdaQueryWrapper<DyOrders> queryWrapper = new MyLambdaQueryWrapper<>();

            queryWrapper.in(DyOrders::getSelfOrder, 1);//平台商品的订单 0未确定 1是 2不是

            queryWrapper.eq(DyOrders::getPutLmStatus, 1);//推送lm状态 0未推送 1已推送 2推送失败

            queryWrapper.eq(DyOrders::getFinalStatus, 2);// 2待发货

            queryWrapper.eq(DyOrders::getShopId, user.getDySellerId());

            queryWrapper.eq(DyOrders::getSendStatus, 0); // 待发货

            queryWrapper.isNotNull(DyOrders::getLmOrderId);

            int page = 1;
            int pageSize = 15;

            while (true) {

                List<DyOrders> ksOrdersList = ksOrdersService.page(queryWrapper, page, pageSize, true).getRecords();

//                System.out.println("这是需要更新状态订单-----");
//                System.out.println(ksOrdersList.toString());
//                System.out.println("这是需要更新状态订单-----");

                if (ksOrdersList.size() > 0) {

                    Set<String> oidset = ksOrdersList.stream().map(DyOrders::getLmOrderId).filter(Objects::nonNull).collect(Collectors.toSet());

                    Map<String, DyOrders> map = ksOrdersList.stream().collect(Collectors.toMap(DyOrders::getLmOrderId, a -> a, (a, o) -> a));

                    QueryOrderListRequest request = new QueryOrderListRequest();
                    request.setBizId(LmConfig.LZ_BIZ_ID);
                    request.setAccountType("ANONY");
                    request.setThirdPartyUserId(user.getDySellerId() + "");
                    request.setBizUid(request.getThirdPartyUserId());
                    request.setPageSize(10L);
                    request.setPageNumber(1L);
                    JSONObject filterOption = new JSONObject();
                    filterOption.put("orderStatus", 2);//"orderStatus":"12=待⽀付，2=已⽀付，4=已退款关闭，6=交易成功，8=被淘宝 关闭 ",
                    filterOption.put("logisticsStatus", 2);//1=未发货 -> 等待卖家发货 2=已发货 -> 等待买家确 认收货 3=已收货 -> 交易成功 4=已经退货 -> 交易失败 5=部分收货 -> 交易成功 6= 部分发货中 8=还未创建物流订单"
                    filterOption.put("enableStatus", 0);
                    filterOption.put("lmOrderList", oidset);
                    request.setFilterOption(filterOption.toJSONString());

             //       System.out.println(JSON.toJSONString(oidset));

                    QueryOrderListResponse response = client.getAcsResponse(request);

                //    System.out.println(JSON.toJSONString(response));

                    List<QueryOrderListResponse.LmOrderListItem> lmOrderList = response.getLmOrderList();

                    lmOrderList.forEach(lmOrderListItem -> {

                        String lmOrderId = lmOrderListItem.getLmOrderId() + "";

                        DyOrders orders = map.get(lmOrderId);
                        if (orders != null) {
                            orders.setSendStatus(1);
                            ksOrdersService.updateById(orders);
                        }
                    });
                }
                if (ksOrdersList.size() < pageSize) {
                    break;
                }
                page += 1;
            }
        }

    }


}
